package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminSetMealController")
@Slf4j
@Api(tags ="套餐管理")
@RequestMapping("/admin/setmeal")
public class SetMealController {
    @Autowired
    private SetMealService setMealService;

    @ApiOperation("分页查询套餐列表")
    @GetMapping("/page")
    public Result page(SetmealPageQueryDTO setmealPageQueryDTO){
        PageResult page = setMealService.page(setmealPageQueryDTO);
        return Result.success(page);
    }

    @ApiOperation("新增套餐")
    @PostMapping
    @CacheEvict(value = "setmeal", key = "#setmealDTO.categoryId")
    public Result add(@RequestBody SetmealDTO setmealDTO){
        setMealService.add(setmealDTO);
        return Result.success();
    }

    @ApiOperation("根据id查询套餐")
    @GetMapping("/{id}")
    public Result getById(@PathVariable String id){
        SetmealVO setmealVO = setMealService.getById(Long.valueOf(id));
        return Result.success(setmealVO);
    }

    @ApiOperation("批量删除套餐")
    @DeleteMapping
    @CacheEvict(value = "setmeal", allEntries = true)
    public Result delete(@RequestParam List<Long> ids){
        setMealService.delete(ids);
        return Result.success();
    }

    @ApiOperation("修改套餐")
    @PutMapping
    @CacheEvict(value = "setmeal", allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO){
        setMealService.update(setmealDTO);
        return Result.success();
    }

    @ApiOperation("启用/禁用套餐")
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmeal", allEntries = true)
    public Result updateStatus(@PathVariable String status,String id){
        setMealService.updateStatus(Long.valueOf(id),Integer.valueOf(status));
        return Result.success();
    }


}
